<?php


namespace app\utils;


class Date
{
    /**
     * 给定年份-第几周，返回此周第一天
     * @param string $date 2021-50
     * @return string|null
     */
    public static function getWeekDate(string $date): ?string
    {
        if (!$date) {
            return null;
        }
        list($year, $weekNum) = explode('-', $date);
        $firstDayOfYear = strtotime($year . '-01-01');
        $firstWeekDay = date('N', $firstDayOfYear);

        if (date('W', $firstDayOfYear) == 1) {    // 是否第一周
            $day = (1 - ($firstWeekDay - 1)) + 7 * ($weekNum - 1);
        } else {
            $day = (9 - $firstWeekDay) + 7 * ($weekNum - 1);
        }
        //$endDate = date('Y-m-d', mktime(0, 0, 0, 1, $day + 6, $year));// 此周结束时间
        return date('Y-m-d', mktime(0, 0, 0, 1, $day, $year));// 此周开始时间
    }

    /**根据年份、周数获取本周的起止日期
     * @param $year
     * @param $weeknum
     * @return array
     */
    public static function getWeekDateRange($year, $weeknum)
    {
        $firstdayofyear = mktime(0, 0, 0, 1, 1, $year);
        $firstweekday = date('N', $firstdayofyear);
        $firstweenum = date('W', $firstdayofyear);
        if ($firstweenum == 1) {
            $day = (1 - ($firstweekday - 1)) + 7 * ($weeknum - 1);
            $startdate = date('Y-m-d', mktime(0, 0, 0, 1, $day, $year));
            $enddate = date('Y-m-d', mktime(0, 0, 0, 1, $day + 6, $year));
        } else {
            $day = (9 - $firstweekday) + 7 * ($weeknum - 1);
            $startdate = date('Y-m-d', mktime(0, 0, 0, 1, $day, $year));
            $enddate = date('Y-m-d', mktime(0, 0, 0, 1, $day + 6, $year));
        }

        return array($startdate, $enddate);
    }

    /**根据日期获取一周的开始结束时间,默认为本周
     * @param null $date
     * @return array
     */
    public static function getWeekDateRangeForDate($date = null)
    {
        $date = $date ?? date("Y-m-d");  //当前⽇期
        $first=1; //$first =1 表⽰每周星期⼀为开始时间 0表⽰每周⽇为开始时间
        $w = date("w", strtotime($date));  //获取当前周的第⼏天 周⽇是 0 周⼀ 到周六是 1 -6
        $d = $w ? $w - $first : 6;  //如果是周⽇ -6天
        $now_start = date("Y-m-d", strtotime("$date -".$d." days")); //本周开始时间
        $now_end = date("Y-m-d", strtotime("$now_start +6 days"));  //本周结束时间

        return array($now_start,$now_end);
    }

    /**
     * 给定时间戳或者年月日格式化时间显示
     * @param string|int $time
     * @return string
     */
    public static function formatDate($time): string
    {
        if (!is_numeric($time)) {
            $time = strtotime($time);
        }
        $diff_time = time() - $time;
        if ($diff_time < 60) {
            $format_data = $diff_time . '秒前';
        } elseif ($diff_time < 3600) {
            $format_data = floor($diff_time / 60) . '分钟前';
        } elseif ($diff_time < 86400) {
            $format_data = floor($diff_time / 3600) . '小时前';
        } else {
            $format_data = date('Y-m-d', $time);
        }
        return $format_data;
    }

    /**根据时间范围获取折线图数据
     * @param $startTime
     * @param $endTime
     * @param $data
     * @return array|false
     */
    public static function getChatDateList($startTime, $endTime, $data)
    {
        if (strtotime($startTime) <= strtotime($endTime)) {
            $dayList = [];
            do {
                $count = $data[date('Ymd', strtotime($startTime))];
                array_push($dayList, ['add_time' => date('Y-m-d', strtotime($startTime)), 'count' => $count ? $count : 0]);
                $startTime = date("Y-m-d", strtotime("+1 day", strtotime($startTime)));
            } while (strtotime($startTime) <= strtotime($endTime));
            return $dayList;
        }
        return false;
    }

    /**
     * 求两个日期之间相差的天数
     * (针对1970年1月1日之后，求之前可以采用泰勒公式)
     * @param string $day1
     * @param string $day2
     * @return number
     */
    public static function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second2 < $second1) {
            $tmp = $second1;
            $second1 = $second2;
            $second2 = $tmp;
        }
        return ($second2 - $second1) / 86400;
    }
}